package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /*
     * 添加菜品*/
    public void addDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        System.out.println(dishDTO);
        BeanUtils.copyProperties(dishDTO, dish);
        System.out.println(dish);
        dishMapper.save(dish);

        Long dishId = dish.getId();

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors.size() > 0 && flavors != null) {
            //向口味表dish_flavor插入n条
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
                System.out.println(dishFlavor);
            });

            //批量插入
            dishFlavorMapper.insertBatch(flavors);
        }


    }

    @Override
    public PageResult dishQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.dishQuery(dishPageQueryDTO);

        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void deletedDishsById(List<Long> ids) {
        //判断是否起售中
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            if (dish.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //判断是否有套餐关联
      List<Long> setMealid=  setmealMapper.getSetmealIdsByDishIds(ids);
        if (setMealid != null && setMealid.size() >0 ){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        for (Long id: ids) {
            dishMapper.deletedById(id);
            //删除口味
            dishFlavorMapper.deletedByDishId(id);
        }

    }

    //根据ID查询菜品
    public DishVO getDishByID(Long id) {
        //查询菜品表
      Dish dish=  dishMapper.getById(id);
        // 查询关联的口味
        List<DishFlavor> dishFlavor = dishFlavorMapper.getFlavorById(id);
        // 封装成VO
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavor);
        return dishVO;
    }

    @Override
    public void  updateDish(DishDTO dishDTO) {
        //修改菜品表dish，执行update操作
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.updateDish(dish);
        // 删除当前菜品关联的口味数据，操作dish_flavor，执行delete操作

        dishFlavorMapper.deletedByDishId(dishDTO.getId());
        //插入最新的口味数据，操作dish_flavor，执行insert操作
        List<DishFlavor> dishFlavors = dishDTO.getFlavors();
        if (dishFlavors != null && dishFlavors.size()>0){
            dishFlavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishDTO.getId());
            });
            dishFlavorMapper.insertBatch(dishFlavors);
        }


    }


    /*菜品的起售和停售*/
    @Override
    public void dishStartOrStop(Integer status,Long id) {

        dishMapper.dishStartOrStop(status,id);
    }

    /*根据类型查询菜品*/
    public List<Dish> queryByList(Long categoryId) {
        Dish dish =  Dish.builder().categoryId(categoryId).status(StatusConstant.ENABLE).build();

        List<Dish> list  =  dishMapper.queryByList(dish);
        return list;
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.queryByList(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getFlavorById(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

}
